/* eslint-disable no-nested-ternary */
import { Ref, reactive, ref, computed, ComputedRef } from 'vue';
import { FilterSearchBarController } from '../filter-search-bar.controller';
import { IFilterData } from '../interface';

export function useFilterModal(c: FilterSearchBarController): {
  visible: Ref<boolean>;
  formData: IData;
  formRef: Ref<IData>;
  formRules: IData;
  tooltip: ComputedRef<{
    title: string;
    label: string;
    placeholder: string;
  }>;
  handleSubmit: () => Promise<void>;
  handleCancel: () => void;
} {
  const visible = ref(false);

  let filter: IFilterData | null = null;

  const mode: Ref<'onNewGroup' | 'onSaveFilter' | 'onRenameFilter'> =
    ref('onNewGroup');

  const tooltip = computed(() => {
    return {
      title:
        mode.value === 'onNewGroup'
          ? '新建分组'
          : mode.value === 'onSaveFilter'
            ? '保存过滤器'
            : '重命名过滤器',
      label: mode.value === 'onNewGroup' ? '分组名称' : '过滤器名称',
      placeholder:
        mode.value === 'onNewGroup' ? '请输入分组名称' : '请输入过滤器名称',
    };
  });

  // 表单数据
  const formData = reactive({
    caption: '',
  });

  // 表单Ref
  const formRef = ref();

  // 表单规则
  const formRules = reactive({
    caption: [{ required: true, message: '名称不能为空', trigger: 'blur' }],
  });

  // 表单取消
  const handleCancel = (): void => {
    if (formRef.value) {
      formRef.value.resetFields();
    }
    visible.value = false;
  };

  c._evt.on('onNewGroup', () => {
    mode.value = 'onNewGroup';
    formData.caption = '';
    visible.value = true;
  });

  c._evt.on('onSaveFilter', event => {
    mode.value = 'onSaveFilter';
    filter = (event as IData).filter;
    formData.caption = '';
    visible.value = true;
  });

  c._evt.on('onRenameFilter', event => {
    mode.value = 'onRenameFilter';
    filter = (event as IData).filter;
    formData.caption = filter?.caption || '';
    visible.value = true;
  });

  // 新建分组表单提交
  const handleSubmit = async (): Promise<void> => {
    if (formRef.value) {
      await formRef.value.validate(async (valid: boolean, _fields: IData[]) => {
        if (valid) {
          if (mode.value === 'onNewGroup') {
            const sameCaptionGroup = c.state.groupFilter.find(group => {
              return group.caption === formData.caption;
            });
            if (sameCaptionGroup) {
              ibiz.message.error('分组名称不能重复！');
              return;
            }
            const newGroup = {
              name: `${new Date().getTime()}`,
              caption: formData.caption,
            };
            await c.onNewGroup(newGroup);
          }
          if (mode.value === 'onSaveFilter' && filter) {
            Object.assign(filter, {
              caption: formData.caption,
              id: `${new Date().getTime()}`,
            });

            await c.onSaveFilter(filter);
          }
          if (mode.value === 'onRenameFilter' && filter) {
            filter.caption = formData.caption;
            await c.onSaveFilter(filter);
          }
          handleCancel();
        }
      });
    }
  };

  return {
    tooltip,
    visible,
    formData,
    formRef,
    formRules,
    handleSubmit,
    handleCancel,
  };
}
